home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / SciAn / src / ScianVisPoints.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  14KB  |  520 lines

  1. /*ScianVisPoints.c
  2.   Sept 23, 
  3.   Eric Pepke
  4.   Point cloud visualization object in SciAn*/
  5.  
  6. #include "Scian.h"
  7. #include "ScianTypes.h"
  8. #include "ScianArrays.h"
  9. #include "ScianWindows.h"
  10. #include "ScianTextBoxes.h"
  11. #include "ScianButtons.h"
  12. #include "ScianTitleBoxes.h"
  13. #include "ScianObjWindows.h"
  14. #include "ScianIcons.h"
  15. #include "ScianColors.h"
  16. #include "ScianControls.h"
  17. #include "ScianLists.h"
  18. #include "ScianSliders.h"
  19. #include "ScianIDs.h"
  20. #include "ScianDatasets.h"
  21. #include "ScianPictures.h"
  22. #include "ScianDialogs.h"
  23. #include "ScianErrors.h"
  24. #include "ScianComplexControls.h"
  25. #include "ScianMethods.h"
  26. #include "ScianStyle.h"
  27. #include "ScianVisObjects.h"
  28. #include "ScianVisPoints.h"
  29. #include "ScianDraw.h"
  30. #include "ScianTemplates.h"
  31. #include "ScianTemplateHelper.h"
  32.  
  33. ObjPtr visPoints;    /*Class for point cloud*/
  34.  
  35. static ObjPtr ChangePointStyle(object)
  36. ObjPtr object;
  37. /*Changed value for a point style flags check box*/
  38. {
  39.     int flagOn;
  40.     ObjPtr val;
  41.     ObjPtr repObj;
  42.     ObjPtr flagsObj, whichFlagObj;
  43.     int flags, whichFlag;
  44.  
  45.     repObj = GetObjectVar("ChangePointStyle", object, REPOBJ);
  46.     if (!repObj)
  47.     {
  48.     return ObjFalse;
  49.     }
  50.  
  51.     flagsObj = GetVar(repObj, POINTSTYLE);
  52.     if (flagsObj)
  53.     {
  54.     flags = GetInt(flagsObj);
  55.     }
  56.     else
  57.     {
  58.     flags = 0;
  59.     }
  60.  
  61.     val = GetVar(object, VALUE);
  62.     if (val)
  63.     {
  64.     flagOn = GetInt(val);
  65.     }
  66.     else
  67.     {
  68.     flagOn = false;
  69.     }
  70.  
  71.     whichFlagObj = GetIntVar("ChangePointStyle", object, WHICHFLAG);
  72.     if (whichFlagObj)
  73.     {
  74.     whichFlag = GetInt(whichFlagObj);
  75.     }
  76.     else
  77.     {
  78.     return ObjFalse;
  79.     }
  80.  
  81.     if (flagOn)
  82.     {
  83.     flags |= whichFlag;
  84.     }
  85.     else
  86.     {
  87.     flags &= ~whichFlag;
  88.     }
  89.  
  90.     SetVar(repObj, POINTSTYLE, NewInt(flags));
  91.     ImInvalid(repObj);
  92.  
  93.     return ObjTrue;
  94. }
  95.  
  96. static ObjPtr AddPointsControls(object, panelContents)
  97. ObjPtr object, panelContents;
  98. /*Adds controls to a points object*/
  99. {
  100.     ObjPtr titleBox, checkBox;
  101.     ObjPtr textBox, corral;
  102.     ObjPtr icon, name, defaultIcon;
  103.     ObjPtr pointsField, mainDataset;
  104.     ObjPtr var;
  105.     int left, right, bottom, top;
  106.     int l, r, b, t;
  107.  
  108.     /*Get the field*/
  109.     pointsField = GetObjectVar("AddPointsControls", object, MAINDATASET);
  110.     if (!pointsField) return ObjFalse;
  111.     while (mainDataset = GetVar(pointsField, MAINDATASET))
  112.     {
  113.     pointsField = mainDataset;
  114.     }
  115.  
  116.  
  117.     /*Put in the source corral at the top left*/
  118.     left = MAJORBORDER;
  119.     top = MAJORBORDER;
  120.     corral = NewIconCorral(NULLOBJ,
  121.                left, left + ONECORRALWIDTH,
  122.                CWINHEIGHT - MAJORBORDER - ONECORRALHEIGHT,
  123.                CWINHEIGHT - MAJORBORDER, 0);
  124.     SetVar(corral, SINGLECORRAL, ObjTrue);
  125.     SetVar(corral, TOPDOWN, ObjTrue);
  126.     SetVar(corral, NAME, NewString("Points Field"));
  127.     SetVar(corral, HELPSTRING,
  128.     NewString("This corral shows the dataset that is being used to make \
  129. the point cloud display."));
  130.     PrefixList(panelContents, corral);
  131.     SetVar(corral, PARENT, panelContents);
  132.     SetVar(corral, REPOBJ, object);
  133.     SetMethod(corral, DROPINCONTENTS, DropInMainDatasetCorral);
  134.  
  135.     /*Create the mesh source text box*/
  136.     textBox = NewTextBox(left, left + ONECORRALWIDTH, 
  137.              CWINHEIGHT - MAJORBORDER - ONECORRALHEIGHT - TEXTBOXSEP - TEXTBOXHEIGHT,
  138.              CWINHEIGHT - MAJORBORDER - ONECORRALHEIGHT - TEXTBOXSEP,
  139.              0, "Points Field Text", "Points Field");
  140.     PrefixList(panelContents, textBox);
  141.     SetVar(textBox, PARENT, panelContents);
  142.     SetTextAlign(textBox, CENTERALIGN);
  143.  
  144.     /*Put in an icon that represents the field*/
  145.     name = GetVar(pointsField, NAME);
  146.     defaultIcon = GetVar(pointsField, DEFAULTICON);
  147.     if (defaultIcon)
  148.     {
  149.     icon = NewObject(defaultIcon, 0);
  150.     SetVar(icon, NAME, name);
  151.     }
  152.     else
  153.     {
  154.     icon = NewIcon(0, 0, ICONQUESTION, GetString(name));
  155.     }
  156.     SetVar(icon, ICONLOC, NULLOBJ);
  157.     SetVar(icon, REPOBJ, pointsField);
  158.     DropIconInCorral(corral, icon);
  159.  
  160.     /*Make check boxes*/
  161.     checkBox = TemplateCheckBox(VisPointCloudTemplate, "Show Missing Data", GetPredicate(object, SHOWMISSING));
  162.     PrefixList(panelContents, checkBox);
  163.     SetVar(checkBox, PARENT, panelContents);
  164.     if (!GetVar(object, SHOWMISSING)) SetVar(object, SHOWMISSING, NewInt(0));
  165.     AssocDirectControlWithVar(checkBox, object, SHOWMISSING);
  166.     SetVar(checkBox, HELPSTRING,
  167.     NewString("When this box is checked, all data points will be shown, \
  168. even if the data point is missing.  When it is not checked, missing data points \
  169. will not be shown."));
  170.  
  171.     checkBox = TemplateCheckBox(VisPointCloudTemplate, "Random Jitter", GetPredicate(object, RANDOMJITTER));
  172.     PrefixList(panelContents, checkBox);
  173.     SetVar(checkBox, PARENT, panelContents);
  174.     if (!GetVar(object, RANDOMJITTER)) SetVar(object, RANDOMJITTER, NewInt(0));
  175.     AssocDirectControlWithVar(checkBox, object, RANDOMJITTER);
  176.     SetVar(checkBox, HELPSTRING,
  177.     NewString("When this box is checked, all data points will be randomly jittered.  \
  178. This can help reduce the regularity of the grid points and give a smoother \
  179. appearance.  Jittered dots take much more time to generate but are displayed \
  180. at the same speed."));
  181.  
  182.     return ObjTrue;
  183. }
  184.  
  185. typedef VertexPtr SixVertices[6];
  186. typedef VertexPtr CubeVertices[2][2][2];
  187.  
  188. static ObjPtr MakePointsSurface(points)
  189. ObjPtr points;
  190. /*Makes the surface of a points object*/
  191. {
  192.     ObjPtr pointsData;
  193.     ObjPtr surface;
  194.     ObjPtr var;
  195.  
  196.     surface = NewPicture();
  197.  
  198.     /*Get the main dataset*/
  199.     MakeVar(points, MAINDATASET);
  200.     pointsData = GetVar(points, MAINDATASET);
  201.  
  202.     if (pointsData)
  203.     {
  204.     /*There is some data.  Put stuff on the surface*/
  205.     int nTraversalDims;        /*Number of dimensions to traverse*/
  206.     int whichDim;            /*Dimension counter*/
  207.     long *traversalDims = 0;    /*Dimensions of the dataset to traverse*/
  208.     long *index = 0;        /*Counting index*/
  209.     long *jitterIndex = 0;        /*Index for jitter*/
  210.     long nNodes;            /*# Total nodes in the dataset*/
  211.     VertexPtr *centerVertices = 0;    /*Center vertices*/
  212.     long nVertices;            /*Number of vertices so far*/
  213.     int nComponents;        /*# components of data form*/
  214.     VertexPtr tv;            /*Temporary vertex*/
  215.     long k;                /*Counter*/
  216.     real x, y, z, d;
  217.     real x1, y1, z1;        /*Points for jittering*/
  218.     real x2, y2, z2;        /*Points for jittering*/
  219.     real w;                /*Jittering weight*/
  220.     PicItemPtr item;
  221.     Bool sameForm;            /*True iff forms are the same*/
  222.     Bool showMissing;        /*True iff show missing data*/
  223.     Bool randomJitter;        /*Random jitter*/
  224.     Bool isMissing;            /*True iff this point is missing*/
  225.  
  226.     showMissing = GetPredicate(points, SHOWMISSING);
  227.     randomJitter = GetPredicate(points, RANDOMJITTER);
  228.  
  229.     SetCurField(FIELD1, pointsData);
  230.     SetCurForm(FORMFIELD, pointsData);
  231.     nComponents = GetNComponents(FORMFIELD);
  232.  
  233.     /*Get the information on traversing the dataset*/
  234.     nTraversalDims = CountTraversalDims(FORMFIELD);
  235.     if (nTraversalDims)
  236.     {
  237.         traversalDims = (long *) Alloc(sizeof(long) * nTraversalDims);
  238.         index = (long *) Alloc(sizeof(long) * nTraversalDims);
  239.         if (randomJitter)
  240.         {
  241.         jitterIndex = (long *) Alloc(sizeof(long) * nTraversalDims);
  242.         srandom(37);
  243.         }
  244.     }
  245.     else
  246.     {
  247.         index = (long *) Alloc(sizeof(long));
  248.     }
  249.     GetTraversalDims(FORMFIELD, traversalDims);
  250.  
  251.     /*Calculate the number of nodes*/
  252.     nNodes = 1;
  253.     for (whichDim = 0; whichDim < nTraversalDims; ++whichDim)
  254.     {
  255.         if (traversalDims[whichDim] > 0)
  256.         {
  257.         nNodes *= traversalDims[whichDim];
  258.         }
  259.     }
  260.  
  261.     /*Make as many vertices as we might need nodes*/
  262.     centerVertices = (VertexPtr *) Alloc(sizeof(VertexPtr) * nNodes);
  263.     if (!centerVertices)
  264.     {
  265.         OMErr();
  266.         return ObjFalse;
  267.     }
  268.  
  269.     /*Fill the vertices*/
  270.  
  271.     nVertices = 0;
  272.  
  273.     /*Zero the index*/
  274.     for (whichDim = 0; whichDim < nTraversalDims; ++whichDim)
  275.     {
  276.         index[whichDim] = 0;
  277.     }
  278.  
  279.     /*Traverse all the points*/
  280.     do
  281.     {
  282.         isMissing = false;
  283.  
  284.         /*See if we need to show the point*/
  285.         if (showMissing)
  286.         {
  287.         }
  288.         else
  289.         {
  290.         /*Hide a missing point*/
  291.         d = SelectFieldScalar(FIELD1, index);
  292.         if (d == missingData)
  293.         {
  294.             isMissing = true;
  295.         }
  296.         }
  297.  
  298.         if (isMissing)
  299.         {
  300.         /*It's missing, who cares what point it is?*/
  301.         x = 0.0;
  302.         y = 0.0;
  303.         z = 0.0;
  304.         }
  305.         else
  306.         {
  307.         /*Sample the location at a point*/
  308.         x = (0 >= nComponents) ? 0.0 : SelectFieldComponent(FORMFIELD, 0, index);
  309.         y = (1 >= nComponents) ? 0.0 : SelectFieldComponent(FORMFIELD, 1, index);
  310.         z = (2 >= nComponents) ? 0.0 : SelectFieldComponent(FORMFIELD, 2, index);
  311.         if (x == missingData)
  312.         {
  313.             isMissing = true;
  314.             x = 0.0;
  315.         }
  316.         if (y == missingData)
  317.         {
  318.             isMissing = true;
  319.             y = 0.0;
  320.         }
  321.         if (z == missingData)
  322.         {
  323.             isMissing = true;
  324.             z = 0.0;
  325.         }
  326.  
  327.         if (randomJitter)
  328.         {
  329.             /*Jitter the point.  First find lower corner*/
  330.             for (whichDim = 0; whichDim < nTraversalDims; ++whichDim)
  331.             {
  332.             if (traversalDims[whichDim] > 0)
  333.             {
  334.                 if (index[whichDim] <= 0)
  335.                 {
  336.                 jitterIndex[whichDim] = 0;
  337.                 }
  338.                 else 
  339.                 {
  340.                 jitterIndex[whichDim] = index[whichDim] - 1;
  341.                 }
  342.             }
  343.             }
  344.             x1 = (0 >= nComponents) ? 0.0 : SelectFieldComponent(FORMFIELD, 0, jitterIndex);
  345.             y1 = (1 >= nComponents) ? 0.0 : SelectFieldComponent(FORMFIELD, 1, jitterIndex);
  346.             z1 = (2 >= nComponents) ? 0.0 : SelectFieldComponent(FORMFIELD, 2, jitterIndex);
  347.             if (x1 == missingData || y1 == missingData || z1 == missingData)
  348.             {
  349.             x1 = x; y1 = y; z1 = z;
  350.             }
  351.             x1 = (x1 + x) * 0.5;
  352.             y1 = (y1 + y) * 0.5;
  353.             z1 = (z1 + z) * 0.5;
  354.  
  355.             /*Now the upper corner*/
  356.             for (whichDim = 0; whichDim < nTraversalDims; ++whichDim)
  357.             {
  358.             if (traversalDims[whichDim] > 0)
  359.             {
  360.                 if (index[whichDim] >= traversalDims[whichDim])
  361.                 {
  362.                 jitterIndex[whichDim] = traversalDims[whichDim];
  363.                 }
  364.                 else 
  365.                 {
  366.                 jitterIndex[whichDim] = index[whichDim] + 1;
  367.                 }
  368.             }
  369.             }
  370.             x2 = (0 >= nComponents) ? 0.0 : SelectFieldComponent(FORMFIELD, 0, jitterIndex);
  371.             y2 = (1 >= nComponents) ? 0.0 : SelectFieldComponent(FORMFIELD, 1, jitterIndex);
  372.             z2 = (2 >= nComponents) ? 0.0 : SelectFieldComponent(FORMFIELD, 2, jitterIndex);
  373.             if (x2 == missingData || y2 == missingData || z2 == missingData)
  374.             {
  375.             x2 = x; y2 = y; z2 = z;
  376.             }
  377.             x2 = (x2 + x) * 0.5;
  378.             y2 = (y2 + y) * 0.5;
  379.             z2 = (z2 + z) * 0.5;
  380.  
  381.             /*Now do a weighted average to get the new point*/
  382.             w = (random() % 255) / 255.0;
  383.             x = x1 * w + x2 * (1.0 - w);
  384.             w = (random() % 255) / 255.0;
  385.             y = y1 * w + y2 * (1.0 - w);
  386.             w = (random() % 255) / 255.0;
  387.             z = z1 * w + z2 * (1.0 - w);
  388.         }
  389.         }
  390.  
  391.         /*Make a vertex*/
  392.         if (nVertices)
  393.         {
  394.         centerVertices[nVertices] = NewVertex(surface, VF_NEXTCANON | (isMissing ? VF_DONTDRAW : 0));
  395.         }
  396.         else
  397.         {
  398.         centerVertices[nVertices] = NewVertex(surface, VF_FIRSTCANON | (isMissing ? VF_DONTDRAW : 0));        
  399.         }
  400.  
  401.         /*Make some center vertices*/
  402.         centerVertices[nVertices] -> position[0] = x;
  403.         centerVertices[nVertices] -> position[1] = y;
  404.         centerVertices[nVertices] -> position[2] = z;
  405.         centerVertices[nVertices] -> normal[0] = 0.0;
  406.         centerVertices[nVertices] -> normal[0] = 0.0;
  407.         centerVertices[nVertices] -> normal[0] = 1.0;
  408.         centerVertices[nVertices] -> colorIndex = 2;
  409.  
  410.         ++nVertices;
  411.  
  412.         /*Advance to next point*/
  413.         for (whichDim = 0; whichDim < nTraversalDims; ++whichDim)
  414.         {
  415.         if (traversalDims[whichDim] > 0)
  416.         {
  417.             if ((++index[whichDim]) >= traversalDims[whichDim])
  418.             {
  419.             index[whichDim] = 0;
  420.             }
  421.             else
  422.             {
  423.             break;
  424.             }
  425.         }
  426.         }
  427.     } while (whichDim < nTraversalDims); /*Break is based on advance*/
  428.     
  429.     /*Make some points*/
  430.     AppendSPolypointToPicture(surface, 1, nVertices, centerVertices);
  431.  
  432.  
  433.     SetVar(surface, MAINDATASET, pointsData);
  434.  
  435.     /*Free up temporary storage*/
  436.     SAFEFREE(traversalDims);
  437.     SAFEFREE(index);
  438.     if (randomJitter)
  439.     {
  440.         SAFEFREE(jitterIndex);
  441.     }
  442.     SAFEFREE(centerVertices);
  443.     }
  444.  
  445.     SetVar(points, SURFACE, surface);
  446.     SetVar(surface, REPOBJ, points);
  447.     return ObjTrue;
  448. }
  449.  
  450. static ObjPtr PointsInit(points)
  451. ObjPtr points;
  452. /*Initializes a points object*/
  453. {
  454.     ObjPtr colorObj;
  455.     ObjPtr pointsField;
  456.  
  457.     MakeVar(points, MAINDATASET);
  458.     SetVar(points, COLOROBJ, colorObj = GetVar(points, MAINDATASET));
  459.     if (colorObj)
  460.     {
  461.     SetVar(points, COLORS, ObjTrue);
  462.     }
  463.  
  464.     return ObjTrue;
  465. }
  466.  
  467. static ObjPtr SetPointsMainDataset(visObj, dataSet)
  468. ObjPtr visObj, dataSet;
  469. /*Sets the main data set of visObj to dataSet*/
  470. {
  471.     SetVar(visObj, MAINDATASET, dataSet);
  472.     return ObjTrue;
  473. }
  474.  
  475. void InitPoints()
  476. /*Initializes points visualization object*/
  477. {
  478.     ObjPtr icon;
  479.  
  480.     /*Class for a point cloud*/
  481.     visPoints = NewObject(visDots, 0);
  482.     AddToReferenceList(visPoints);
  483.     SetVar(visPoints, NAME, NewString("Point Cloud"));
  484.     SetMethod(visPoints, INITIALIZE, PointsInit);
  485.     SetVar(visPoints, DEFAULTICON, icon = NewObject(visIcon, 0));
  486.     SetVar(icon, WHICHICON, NewInt(ICONPOINTS));
  487.     SetVar(icon, NAME, NewString("Point Cloud"));
  488.     SetVar(icon, HELPSTRING,
  489.     NewString("This icon represents a point cloud or ball display object.  \
  490. This object can show a point cloud or group of balls for just about any data field."));
  491.     DeclareIndirectDependency(visPoints, SURFACE, MAINDATASET, CHANGED);
  492.     DeclareIndirectDependency(visPoints, SURFACE, MAINDATASET);
  493.     SetVar(visPoints, SHOWMISSING, ObjFalse);
  494.     SetVar(visPoints, RANDOMJITTER, ObjFalse);
  495.     DeclareDependency(visPoints, SURFACE, SHOWMISSING);
  496.     DeclareDependency(visPoints, SURFACE, RANDOMJITTER);
  497.     SetVar(visPoints, DRAWDOTS, ObjTrue);
  498.     SetMethod(visPoints, SURFACE, MakePointsSurface);
  499.     DeclareDependency(visPoints, SURFACE, POINTSTYLE);
  500.     SetMethod(visPoints, SETMAINDATASET, SetPointsMainDataset);
  501.     SetVar(visPoints, STYLE, NewInt(0));
  502.  
  503.     SetMethod(visPoints, ADDCONTROLS, AddPointsControls);
  504.     icon = NewIcon(0, 0, ICONPOINTS, "Point Cloud");
  505.     SetVar(icon, HELPSTRING,
  506.     NewString("Click on this icon to see a panel of controls for the point object."));
  507.     SetVar(visPoints, CONTROLICON, icon);
  508.  
  509.     DefineVisMapping(DS_HASFORM | DS_HASFIELD | DS_VECTOR, -1, -1, -1, visPoints);
  510.     DefineVisMapping(DS_HASFORM | DS_HASFIELD, -1, -1, -1, visPoints);
  511.     DefineVisMapping(DS_HASFORM | DS_HASFIELD | DS_VECTOR | DS_UNSTRUCTURED, -1, -1, -1, visPoints);
  512.     DefineVisMapping(DS_HASFORM | DS_HASFIELD | DS_UNSTRUCTURED, -1, -1, -1, visPoints);
  513. }
  514.  
  515. void KillPoints()
  516. /*Kills points visualization*/
  517. {
  518.     DeleteThing(visPoints);
  519. }
  520.